package org.balthie.demo.opensource.org.reflections;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Set;
import org.reflections.Reflections;
import org.reflections.scanners.MethodAnnotationsScanner;
import org.reflections.scanners.MethodParameterScanner;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.scanners.TypeAnnotationsScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.FilterBuilder;

//https://github.com/ronmamo/reflections
public class ReflectionsDemo1
{
    public static void main(String[] args) throws NoSuchMethodException, SecurityException
    {
//        // 实例化Reflections，并指定要扫描的包名
//        Reflections defaultReflections = new Reflections("org.balthie.demo.my");
//        // 获取某个类的所有子类
//        Set<Class<? extends HashMap>> subTypes = defaultReflections.getSubTypesOf(HashMap.class);
//        System.out.println(subTypes);
//        
//        // 获取包含某个注解的所有类
//        Set<Class<?>> annotated = defaultReflections.getTypesAnnotatedWith(Deprecated.class);
//        System.out.println(annotated);
        
        // MethodAnnotationsScanner 扫描方法注解
        Reflections typeAnnotations =  new Reflections(new ConfigurationBuilder()
                .setUrls(ClasspathHelper.forPackage("org.balthie.demo"))
                .setScanners(new SubTypesScanner(), 
                             new TypeAnnotationsScanner().filterResultsBy(new FilterBuilder().exclude(Object.class.getName())),
                             new MethodAnnotationsScanner().filterResultsBy(new FilterBuilder().include("java").add(new FilterBuilder().include("org.balthie"))),
                             new MethodParameterScanner().filterResultsBy(new FilterBuilder().include("java").add(new FilterBuilder().include("org.balthie")))
                             )
                .filterInputsBy(new FilterBuilder().includePackage("org.balthie.demo.my", "org.balthie.demo.jdk")));
        
        Set<Method> methods = typeAnnotations.getMethodsAnnotatedWith(Override.class);
        System.out.println(methods);
        Set<Constructor> depConst = typeAnnotations.getConstructorsAnnotatedWith(Deprecated.class);
        System.out.println(depConst);
        
      //MethodParameterScanner 扫描方法参数
        Set<Method> someMethods =
                typeAnnotations.getMethodsMatchParams(long.class, int.class);
        System.out.println(someMethods);
        Set<Method> voidMethods =
                typeAnnotations.getMethodsReturn(void.class);
        System.out.println(voidMethods);
        Set<Method> pathParamMethods =
                typeAnnotations.getMethodsWithAnyParamAnnotated(Deprecated.class);
        System.out.println(pathParamMethods);
        
        //MemberUsageScanner 扫描方法调用
//        Set<Member> usages = typeAnnotations.getMethodUsage(Object.class.getDeclaredMethod("toString"));
    }
    
}
